//
//  KVCVC.m
//  demo
//
//  Created by chenzm on 2018/8/17.
//  Copyright © 2018年 chenzm. All rights reserved.
//

#import "KVCVC.h"
#import "Author.h"
#import "Book.h"

//KVC：就是可以暴力的去get/set类的私有属性，同时还有强大的键值路径对数组类型的属性进行操作
@interface KVCVC ()

@end

@implementation KVCVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    // Do any additional setup after loading the view.
}

-(void)buttonClick:(UIButton *)sender{
    //----------    KVC的运算    --------
    Author *author = [[Author alloc] init];
    //设置属性值
    [author setValue:@"莫言" forKeyPath:@"name"];
    
    //键值编码取属性的值
    NSString *name = [author valueForKey:@"name"];
    NSLog(@"%@",name);

    
    //-------   KVC设置作者的书籍数组   -------
    //键值路径:对于一个类中有数组对象的属性进行便捷操作
    Book *book1 = [[Book alloc] init];
    book1.name = @"红高粱";
    book1.price = 9;
    Book *book2 = [[Book alloc] init];
    book2.name = @"蛙";
    book2.price = 6;
    NSArray *array = [NSArray arrayWithObjects:book1,book2, nil];
    [author setValue:array forKeyPath:@"issueBook"];
    
    
    // -----    KVC中键值路径取值   -------
    //基本数据类型会自动被包装成NSNumber，装到数组中
    //得到所有书籍的价格
    NSArray *priceArray = [author valueForKeyPath:@"issueBook.price"];
    NSLog(@"%@",priceArray);
    
    //获取数组的大小
    NSNumber *count = [author valueForKeyPath:@"issueBook.@count"];
    NSLog(@"count=%@",count);
    
    //获取书籍价格的总和
    NSNumber *sum = [author valueForKeyPath:@"issueBook.@sum.price"];
    NSLog(@"%@",sum);
    //获取书籍的平均值
    NSNumber *avg = [author valueForKeyPath:@"issueBook.@avg.price"];
    NSLog(@"%@",avg);
    //获取书籍的价格最大值和最小值
    NSNumber *max = [author valueForKeyPath:@"issueBook.@max.price"];
    NSNumber *min = [author valueForKeyPath:@"issueBook.@min.price"];
    NSLog(@"%@____%@",max,min);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
